package com.mlz.mlzmall.ui.utils.util;

/**
 * describe :
 * Created by Benny on 2016/5/9.
 * PackageName com.mlz.mall.ui.utils.util.
 * ProjectName mlzMall.
 */

import android.content.Context;
import android.content.SharedPreferences;

/**
 * @author andong
 * 缓存类, 所有的数据都是采用SharedPreferences方式存储和获取.
 */
public class CacheUtils {

    private static final String CACHE_FILE_NAME = "mlzMall";
    private static SharedPreferences sp;

    /**
     * @param context
     * @param key 要取的数据的键
     * @param defValue 缺省值
     * @return
     */
    public static boolean getBoolean(Context context, String key, boolean defValue) {
        if(sp == null) {
            sp = context.getSharedPreferences(CACHE_FILE_NAME, Context.MODE_PRIVATE);
        }
        return sp.getBoolean(key, defValue);
    }

    /**
     * 存储一个boolean类型数据
     * @param context
     * @param key
     * @param value
     */
    public static void putBoolean(Context context, String key, boolean value) {
        if(sp == null) {
            sp = context.getSharedPreferences(CACHE_FILE_NAME, Context.MODE_PRIVATE);
        }
        sp.edit().putBoolean(key, value).commit();
    }

    /**
     * 存储一个String类型的数据
     * @param context
     * @param key
     * @param value
     */
    public static void putString(Context context, String key, String value) {
        if(sp == null) {
            sp = context.getSharedPreferences(CACHE_FILE_NAME, Context.MODE_PRIVATE);
        }
        sp.edit().putString(key, value).commit();
    }

    /**
     * 根据key取出一个String类型的值
     * @param context
     * @param key
     * @param defValue
     * @return
     */
    public static String getString(Context context, String key, String defValue) {
        if(sp == null) {
            sp = context.getSharedPreferences(CACHE_FILE_NAME, Context.MODE_PRIVATE);
        }
        return sp.getString(key, defValue);
    }
}
